<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="/web/res/css/xadmin.css">
        <script src="/web/res/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="/web/res/js/xadmin.js"></script>
</head>

<body style="background-color: white;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend class="form-title"><div id="tip">【${organ.title}】</div></legend>
    </fieldset>
    <div class="layui-card-header">
        <button class="layui-btn layui-btn-danger layui-btn-sm addMeNu">
            <i class="layui-icon layui-icon-add-1"></i>添加下级
        </button>
        <a class="layui-btn layui-btn-small lable-text-center"
           style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()"
           title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>刷新表格数据</a>
    </div>

    <div class="layui-fluid" id="orderEdit">
        <form class="layui-form layui-form-pane"  id="formsubmit" action="/organ/save">
            <input type="hidden" name="id" id="id" value="${organ.id}" />
            <input type="hidden" name="pid" id="pid" value="${organ.pid}" />
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">组织机构名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" value="${organ.title}" required lay-verify="required" placeholder="请输入组织机构名称"  autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">排序值</label>
                    <div class="layui-input-inline">
                        <input type="text" name="sorts" required lay-verify="required|number" autocomplete="off" class="layui-input" value="${organ.sorts!''}">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label lable-text-center">是否有效</label>
                    <div class="layui-input-inline">
                        <input type="checkbox" name="states" value="1" lay-skin="switch" lay-text="启用|不启用"  <#if organ.states?exists && (organ.states)?number == 1>checked</#if>/>
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注信息</label>
                <div class="layui-input-block">
                    <textarea name="comments" class="layui-textarea">${organ.comments!''}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-radius" lay-submit lay-filter="save">&emsp;保存&emsp;</button>
                    <button type="button" class="layui-btn layui-btn-primary layui-btn-radius delorgan" id="delorgan" >&emsp;删除&emsp;</button>
                </div>
            </div>
        </form>
    </div>
    <script>
        layui.use('form', function () {
            let $ = layui.$;
            $.fn.extend({
                serializeObject: function() {
                    var obj = {}; //声明一个对象
                    $.each(this.serializeArray(), function(index, field) {
                        obj[field.name] = field.value; //通过变量，将属性值，属性一起放到对象中
                    })
                    return obj;
                }
            });
            var form = layui.form;
            form.render();

            // 添加下级组织机构
            $('.layui-card-header').on('click', '.addMeNu', function () {
                var pid = $("#id").val();
                $(':input','#formsubmit')
                    .not(':button, :submit, :reset,:checkbox')
                    .val('');
                $('#pid').val(pid);
                $('#delorgan').remove();
                $('div[api]').each(function(index,ele){
                    // 先获取内部元素
                    if(index!=0){
                        ele.remove();
                    }
                });
                $('#tip').text("正在添加 "+$('#tip').text()+" 的下级组织机构");

                return false;
            });

            // 删除组织机构
            $('.layui-form-item').on('click', '.delorgan', function () {
                var id = $("#id").val();
                layer.confirm('确认要删除吗？', function (index) {
                    ajaxPost(layui.$,"/organ/del",{"id":id},function (data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                                window.parent.freshref(${organ.pid});
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 1000}, function(){
                            });
                        }
                    });
                });
            });

            form.on('submit(save)', function(data){
                layer.confirm('确认要保存吗？', function (index) {
                    ajaxPost($,$("#formsubmit").attr("action"),$("#formsubmit").serializeObject(),function (data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                                window.parent.freshref(${organ.id});
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 1000}, function(){
                                xadmin.close();
                            });

                        }
                    });
                });
            });

        });


    </script>
</body>

</html>